
/* Generated by Interface Builder */

#import "IPref.h"

@implementation IPref

- printerOff:sender
{
  printeroffF = !printeroffF;
  
  return self;
}

- cshrc:sender
{
  cshrcF = !cshrcF;

  return self;
}

- ilogspace:sender
{
  ilogspaceF = !ilogspaceF;
  
  return self;
}

- login:sender
{
  loginF = !loginF;
  
   return self;
 }

- updateButtons
{
  [printerOff setState:printeroffF];
  [cshrc setState:cshrcF];
  [login setState:loginF];
  [ilogspace setState:ilogspaceF];

  return self;
}

void doCSH(cmd)
     char *cmd;
{
  FILE *fd;
  
  fd = popen(cmd, "r");
  pclose(fd);
}

- doTasks
{
  char cmd[256]; /* more than enough to hold the command */

  cmd[0]='\0';
  
  if (printeroffF) fprintf(stderr, "turn printer off\n");

  if (cshrcF) sprintf(cmd, "csh > /dev/console");
  if (loginF) sprintf(cmd, "csh -f ~/.login > /dev/console");
  if (loginF && cshrcF) sprintf(cmd, "csh ~/.login > /dev/console");
  doCSH(cmd);

  if (ilogspaceF) sprintf(cmd, "csh -f ~/.ilogspace > /dev/console");
  doCSH(cmd);

  return self;
}


- set:sender
 {
   /* this should really be done as a vector passed to NXWriteDefaults
    * but I never bothered to figure out the c syntax for such a
    * creature...  Admittedly, I'm a bum.
    */
   NXWriteDefault(appName, "cshrc", booltostr(cshrcF));
   NXWriteDefault(appName, "ilogspace", booltostr(ilogspaceF));
   NXWriteDefault(appName, "login", booltostr(loginF));
   NXWriteDefault(appName, "printeroff", booltostr(printeroffF));
   NXUpdateDefaults();

   return self;
 }

- cancel:sender
{
  [self readDefaults];
  
  return self;
}

 - readDefaults
{
  printeroffF = strtobool(NXGetDefaultValue(appName, "printeroff"));
  loginF      = strtobool(NXGetDefaultValue(appName, "login"));
  cshrcF      = strtobool(NXGetDefaultValue(appName, "cshrc"));
  ilogspaceF  = strtobool(NXGetDefaultValue(appName, "ilogspace"));

  [self updateButtons];

  return self;
}

 - appDidInit:sender
 {
   static NXDefaultsVector iLogDefaults = {
     {"login", "NO"},
     {"cshrc", "NO"},
     {"ilogspace", "NO"},
     {"printeroff", "NO"},
     { NULL, NULL }
   };
   appName = "iLog";

   NXRegisterDefaults(appName, iLogDefaults);

   [self readDefaults];
   [self doTasks];
   [self updateButtons];

   return self;
}
@end
